<?php
/**
 * @package		简易CMS
 * @subpackage	后台工具箱
 * @version		$Id: admincp_tools.php 25 2012-08-29 17:55:19Z htmambo@gmail.com $
 * @author		Hoping
 * @copyright	Copyright (C) 2010 Hoping Software Studio.
 */

if(!defined('IN_JYCMS') || !defined('IN_ADMINCP')) {
	exit('Access Denied');
}

cpheader();

if($operation == 'updatecache') {

	$step = max(1, intval($_G['gp_step']));
	shownav('tools', 'nav_updatecache');
	showsubmenusteps('nav_updatecache', array(
		array('nav_updatecache_confirm', $step == 1),
		array('nav_updatecache_verify', $step == 2),
		array('nav_updatecache_completed', $step == 3)
	));

	showtips('tools_updatecache_tips');

	if($step == 1) {
		cpmsg("<input type=\"checkbox\" name=\"type[]\" value=\"data\" id=\"datacache\" class=\"checkbox\" checked /><label for=\"datacache\">".cplang('tools_updatecache_data')."</label><input type=\"checkbox\" name=\"type[]\" value=\"tpl\" id=\"tplcache\" class=\"checkbox\" checked /><label for=\"tplcache\">".cplang('tools_updatecache_tpl')."</label><input type=\"checkbox\" name=\"type[]\" value=\"blockclass\" id=\"blockclasscache\" class=\"checkbox\" /><label for=\"blockclasscache\">".cplang('tools_updatecache_blockclass').'</label>', 'action=tools&operation=updatecache&step=2', 'form', '', FALSE);
	} elseif($step == 2) {
		$type = implode('_', (array)$_G['gp_type']);
		cpmsg(cplang('tools_updatecache_waiting'), "action=tools&operation=updatecache&step=3&type=$type", 'loading', '', FALSE);
	} elseif($step == 3) {
		$type = explode('_', $_G['gp_type']);
		if(in_array('tpl', (array)$type) && $_G['config']['output']['tplrefresh']) {
			cmsFS::deleteFolder(ROOT_PATH.'./data/template', false);
			cmsFS::deleteFolder(ROOT_PATH.'/data/cache/css', false);
			cmsFS::deleteFolder(ROOT_PATH.'/data/cache/js', false);
		}
		updatecache();
		if(in_array('blockclass', (array)$type)) {
			cmsFS::deleteFolder(ROOT_PATH.'/data/cache/block', false);
			include_once libfile('function/block');
			blockclass_cache();
		}
		cpmsg('update_cache_succeed', '', 'succeed', '', FALSE);
	}

} elseif($operation == 'fileperms') {

	$step = max(1, intval($_G['gp_step']));

	shownav('tools', 'nav_fileperms');
	showsubmenusteps('nav_fileperms', array(
		array('nav_fileperms_confirm', $step == 1),
		array('nav_fileperms_verify', $step == 2),
		array('nav_fileperms_completed', $step == 3)
	));

	if($step == 1) {
		cpmsg(cplang('fileperms_check_note'), 'action=tools&operation=fileperms&step=2', 'button', '', FALSE);
	} elseif($step == 2) {
		cpmsg(cplang('fileperms_check_waiting'), 'action=tools&operation=fileperms&step=3', 'loading', '', FALSE);
	} elseif($step == 3) {

		showtips('fileperms_tips');

		$entryarray = array(
			'data',
			'data/attachment',
			'data/cache',
			'data/log',
			'data/template',
			'data/diy',
		);

		foreach(array('data/cache', 'data/log', 'data/template', 'data/attachment', 'data/diy') as $directory) {
			getdirentry($directory);
		}

		$result = '';
		foreach((array)$entryarray as $entry) {
			$fullentry = ROOT_PATH.'./'.$entry;
			if(!is_dir($fullentry) && !file_exists($fullentry)) {
				continue;
			} else {
				if(!is_writeable($fullentry)) {
					$result .= '<li class="error">'.(is_dir($fullentry) ? cplang('dir') : cplang('file'))." ./$entry ".cplang('fileperms_unwritable')."</li>";
				}
			}
		}
		$result = $result ? $result : '<li>'.cplang('fileperms_check_ok').'</li>';
		echo '<div class="colorbox"><ul class="fileperms">'.$result.'</ul></div>';
	}
}

function getdirentry($directory) {
	global $entryarray;
	if(!file_exists(ROOT_PATH.'./'.$directory)) {
		return;
	}
	$dir = dir(ROOT_PATH.'./'.$directory);
	while($entry = $dir->read()) {
		if(!in_array($entry, array('.', '..', 'index.htm'))) {
			if(is_dir(ROOT_PATH.'./'.$directory.'/'.$entry)) {
				getdirentry($directory."/".$entry);
			}
			$entryarray[] = $directory.'/'.$entry;
		}
	}
	$dir->close();
}

function jsinsertunit() {

?>
<script type="text/JavaScript">
function isUndefined(variable) {
	return typeof variable == 'undefined' ? true : false;
}

function insertunit(text, obj) {
	if(!obj) {
		obj = 'jstemplate';
	}
	$$$(obj).focus();
	if(!isUndefined($$$(obj).selectionStart)) {
		var opn = $$$(obj).selectionStart + 0;
		$$$(obj).value = $$$(obj).value.substr(0, $$$(obj).selectionStart) + text + $$$(obj).value.substr($$$(obj).selectionEnd);
	} else if(document.selection && document.selection.createRange) {
		var sel = document.selection.createRange();
		sel.text = text.replace(/\r?\n/g, '\r\n');
		sel.moveStart('character', -strlen(text));
	} else {
		$$$(obj).value += text;
	}
}
</script>
<?php

}
